package thread;

public class Demo7 {
    private static final long count = 10_0000_0000L;
    public static void serial (){
        long beg = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0;
        for (int i = 0; i < count; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+ (end - beg) + "ms");
    }

    public static void concurrency() throws InterruptedException {
        long beg = System.currentTimeMillis();
        Thread t1 = new Thread(()->{
            long a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            long b = 0;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        t2.start();

        // 次数不能直接记录结束时间，现在这个求时间戳的代码 是在 main 线程中
        // main和 t1 t2 之间是并行执行的关系，此处t1和t2 还没执行完呢，这里就开始记录结束时间了，这显然是不准确的
        // 正确做法应该是 main线程等待 t1和t2跑完了，再来记录结束时间
        // join 效果就是等待线程结束，t1.join 就是让 main 线程等待 t1结束 t2.join让 main 线程等待 t2结束
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+ (end - beg) + "ms");
    }

    public static void main(String[] args) throws InterruptedException {
        serial();
        // concurrency();
    }

}
